const App = getApp();
import Notify from '../../../dist/notify/notify';
import Toast from '../../../dist/toast/toast';

Page({
  data: {
    imgUrl: App.globalData.imgUrl,
    isPageType: 1,
    isLoading: false,
    reLoading: false,
    features_id: 0,
    hara_url: '',
    ad_img: '',
    file_id: 0,
    tmp_img: '',
    wechatUrl: '',
    aliUrl: '',
    share_title: '',
    ad_img_list: [],
    inputLists: [{}],
    videoSrc: '',
    videoType: '',
    replace: {
      file: '',
      text: '',
    },
    videoAd: null,
    isArea: false,
    isTipsShow: false,
    switch1Checked: false,
    switch2Checked: false,
    radioChecked: 'r1',
    areaColumns: [],
    wxAreaList: [],
    areaData: {},
    originalId: "",
    unionCode: "",
    imageAd: '',//广告引导页默认图片
  },
  confirmArea(e) {
    this.setData({
      isArea: false,
      "areaData.name": e.detail.value,
      "areaData.id": this.data.wxAreaList[e.detail.index].id
    })
    console.log(this.data.areaData)
  },
  cancelArea() {
    this.setData({
      isArea: false
    })
  },
  clickArea() {
    let temp = !this.data.isArea
    this.setData({
      isArea: temp
    })
  },
  onLoad: function (options) {
    if (options && options.areaId) {
      this.setData({
        "areaData.id": options.areaId
      })
    }
    console.log(this.data.areaData.id)
    // this.setData({
    //   'detail.guide.file_path': 'https://fn.w7.axa2.com/addons/empower_tool/web/uploads/20231219105022845f81971.png',
    //   tmp_img: 'https://fn.w7.axa2.com/addons/empower_tool/web/uploads/20231219105022845f81971.png',
    //   ad_img: 'https://fn.w7.axa2.com/addons/empower_tool/web/uploads/20231219105022845f81971.png'
    // })
    this.getWxAreaList()
    this.getAdLeadTo()
  },
  onShow() {

  },
  pageType() {
    let e = this;
    this.setData({
      isPageType: e.data.isPageType == 1 ? 2 : 1
    })
    // console.log(e.data.isPageType)
  },
  radioChange(e) {
    // console.log(e);
    this.setData({
      radioChecked: e.detail.value
    })
  },
  // 验证原始id
  checkGh(e) {
    console.log(e.detail.value)
    if (e.detail.value.indexOf('gh_') == -1) {
      Notify({
        type: 'primary',
        message: '小程序原始ID不正确'
      });
      return false
    }
  },
  // 扫原码
  scanning() {
    let _this = this;
    _this.setData({
      reLoading: true
    })
    wx.scanCode({
      success(res) {
        if (res.result != "*") {
          _this.setData({
            hara_url: res.result
          })
          console.log(res.result);
        } else {
          wx.showToast({
            title: '扫码失败',
            icon: 'none'
          })
          console.log("扫码错误" + res.result);
        }
      },
      fail(res) {
        _this.setData({
          hara_url: ''
        })
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        })
      },
      complete() {
        _this.setData({
          reLoading: false
        })
      },
    })
  },
  // 扫空白码
  blankScanning() {
    let _this = this;
    _this.setData({
      reLoading: true
    })
    wx.scanCode({
      success(res) {
        let url = res.result
        console.log(res);
        if (res.result.indexOf('https://fn.w7.axa2.com') != -1) {
          url = url.substring(url.length - 6);
          // console.log(url)
          _this.setData({
            unionCode: url
          })
        } else {
          wx.showModal({
            title: '温馨提示',
            content: '请选择正确的空白码!',
            showCancel: false,
          })
        }
      },
      fail(res) {
        _this.setData({
          unionCode: ''
        })
        wx.showToast({
          title: '扫码失败',
          icon: 'none'
        })
      },
      complete() {
        _this.setData({
          reLoading: false
        })
      },
    })
  },
  // 引导页图片上传
  chooseImage() {
    let _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res.tempFilePaths[0]);
        _this.setData({
          'detail.guide.file_path': res.tempFilePaths[0],
          tmp_img: res.tempFilePaths[0],
        })
        _this.uploadFile();
      }
    });
  },
  // 执行图片上传
  uploadFile() {
    let _this = this;
    let params = {
      wxapp_id: App.getWxappId(),
      token: wx.getStorageSync('token')
    };
    _this.setData({
      reLoading: true,
    })
    wx.uploadFile({
      url: App.api_root + 'upload/image',
      filePath: _this.data.tmp_img,
      name: 'iFile',
      formData: params,
      success: function (res) {
        let result = typeof res.data === "object" ? res.data : JSON.parse(res.data);
        _this.setData({
          file_id: result.data.file_id,
          ad_img: result.data.file_path,
          reLoading: false,
        })
      }
    });
  },

  // 提交赋能
  submit(e) {
    // console.log(e)
    let _this = this,
      value = e.detail.value;
    // 表单校验
    if (!_this._onVerify(value)) {
      return false
    }
    // 二维码扫码赋能需验证自定义中转页
    // if (_this.data.detail.type.value == 20 && !_this.data.tmp_img.length) {
    //     // 否则使用默认的引导页
    //     _this.setData({
    //         file_id: _this.data.detail.guide.file_id
    //     })
    // }

    if (_this.data.radioChecked == 'r1') {
      value.hara_url = _this.data.hara_url;
    } else {

    }
    value.type = _this.data.radioChecked == 'r1' ? 1 : 2;
    value.areaId = _this.data.areaData.id;
    value.unionCode = _this.data.unionCode;
    value.ad_img = _this.data.ad_img;
    // 提交表单
    _this.setData({
      reLoading: true,
    })
    console.log(value, 'value', _this.data.ad_img)
    App._post_form('Advertisement/enabling', value, res => {
      if (res.code == 201) {
        // wx.showModal({
        //     title: '温馨提示',
        //     content: '您的次数不足且不是VIP身份!',
        //     showCancel: false,
        //     success(res) {}
        // })

      } else {
        console.log(res)
        App.showSuccess("操作成功!", () => {
          wx.redirectTo({
            url: '/pages/salesman/mine/mine'
          })
        })
      }
    }, false, () => {
      _this.setData({
        reLoading: false
      })
    })

  },


  // 表单验证
  _onVerify(data) {
    let _this = this;
    // type 1
    if (_this.data.radioChecked == "r1" && !_this.data.hara_url.length) {
      Notify({
        type: 'primary',
        message: '请扫描源码'
      });
      return false
    }

    // type 2
    if (_this.data.radioChecked == "r2") {
      if (data.originalId.indexOf('gh_') == -1 || !data.originalId) {
        Notify({
          type: 'primary',
          message: '小程序原始ID不正确'
        });
        return false
      }
      if (!data.siteLink.length) {
        Notify({
          type: 'primary',
          message: '请填写小程序页面路径'
        });
        return false
      }
      // let reg = /(http|https):\/\/([\w.]+\/?)\S*/;
      // if (!reg.test(data.siteLink)) {
      //     Notify({
      //         type: 'primary',
      //         message: '网站链接不是一个标准的url格式'
      //     });
      //     return false
      // }
    }


    if (!_this.data.unionCode) {
      Notify({
        type: 'primary',
        message: '请扫描空白码'
      });
      return false
    }

    if (!_this.data.areaData.id) {
      Notify({
        type: 'primary',
        message: '请选择区域'
      });
      return false
    }

    return true;
  },
  //获取区域
  getWxAreaList() {
    let _this = this;
    App._get('Area/WxAreaList', {

    }, res => {
      let list = [];
      res.data.map(item => {
        list.push(item.f_area_name)
      })

      _this.setData({
        wxAreaList: res.data,
        areaColumns: list
      })
    }, false, () => {

    })
  },
  //获取广告引导页默认图片
  getAdLeadTo() {
    let _this = this;
    App._get('Area/AdLeadTo', {

    }, res => {
      console.log(res, 'reeeeeee')
      if (res.code == 1) {
        // _this.setData({
        //   imageAd: res.data.image
        // })
        _this.setData({
          'detail.guide.file_path': res.data.image,
          tmp_img: res.data.image,
          ad_img: res.data.image
        })
      }

    }, false, () => {

    })
  }
});